
//
//  LYRecomendView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/22.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYRecomendView.h"
#import "LYRecomendCell.h"

@interface LYRecomendView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation LYRecomendView

- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)itemDataArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
        [self.dataArray addObjectsFromArray:itemDataArray];
        [self createSubview];
    }
    return self;
}

- (void)createSubview{
    self.index = 0;
    CGFloat height = self.dataArray.count *45;
    if (height > 300) {
        height = 300;
    }
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HEIGHT,height) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    [self addSubview:self.tableview];
    
    [self.tableview registerClass:[LYRecomendCell class] forCellReuseIdentifier:kCellIdentifier_LYRecomendCell];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYRecomendCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYRecomendCell forIndexPath:indexPath];
    [cell reloadData:self.dataArray[indexPath.row] isSelect:(self.index == indexPath.row)?YES:NO];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.index = indexPath.row;
    [tableView reloadData];
//    NSDictionary *dic = self.dataArray[indexPath.row];
//    _block(dic);
    
    PublicCategoryElementModel *model = self.dataArray[indexPath.row];
    self.backBlock(model);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}


@end
